<%--
  Created by IntelliJ IDEA.
  User: xiaoxiang
  Date: 2021/10/25
  Time: 14:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>首页</title>
    <script src="${pageContext.request.contextPath}/static/js/jquery-3.4.1.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/tools.js"></script>
</head>
<body>

<%
    Cookie[] cook = request.getCookies();//获取本地所有的Cookie对象
    String[] userMessage = new String[] { "", "" }; //用户存储从cookie中获取的用户名和密码
    if (cook != null) {
        for (int i = 0; i < cook.length; i++) {
            if (cook[i].getName().equals("userInfo")) {
                userMessage = cook[i].getValue().split("#");//通过分割split获取用户名和密码.
            }

        }
    }
%>



<div>
    <div align="left">
        用户名： <input id="username" type="text" name="username" value="<%=userMessage[0]%>">
        密&nbsp;&nbsp;&nbsp;&nbsp;码：<input id="password" type="passwod" name="password" value="<%=userMessage[1]%>">

    </div><br />
    <input type="button" value="登录" onclick="">
</div>


<script>
    function login() {
        var json = {
            userid: "",
            username: document.getElementById("username").value,
            password: document.getElementById("password").value
        };
        var postdata = JSON.stringify(json);
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "login.do",
            dataType: "json",
            data: postdata,
            success: function(data) {
                if(data.userid == 0){
                    alert("用户名或密码错误!!!")
                }
                else{

                    document.cookie="userid="+data.userid;
                    //alert(data.userid)
                    alert(getCookie('userid'))
                }
                /*
                alert(
                    "username : " + data.username + "\npassword : " + data.password
                );*/
            },
            error: function() {
                alert("error...");
            }
        });
    }
</script>

</body>
</html>
